#! /bin/sh

#####################################
# PREPARE variables:
#
# NOTE that "app"/Contents specify slash. 
# this is not a problem even if app itself have slash at end.
# We will get something as  gg.app//Contents and this still works fine.
#
app=$1
app_vcomp="$app"/Contents/vcomponents


#####################################
# COPY files of vcomponents into APP.
#####################################

if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvshared_fat_release.dylib "$app_vcomp"
fi

# VKERNEL
if [ ! -e "$app_vcomp"/libvkernel_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvkernel_fat_release.dylib "$app_vcomp"
fi

# VCLIENT
if [ ! -e "$app_vcomp"/libvclient_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvclient_fat_release.dylib "$app_vcomp"
fi

# VREPORT
if [ ! -e "$app_vcomp"/libvreport_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvreport_fat_release.dylib "$app_vcomp"
fi

# WX (required for VREPORT only)
# DEPRECATED in v4.8
#
if [ -e /usr/local/lib/vcomponents/libwx_mac.dylib ] ; then
	if [ ! -e "$app_vcomp"/libwx_mac.dylib ] ; then
		cp /usr/local/lib/vcomponents/libwx_mac.dylib 			"$app_vcomp"
	fi
fi


####################################
# Copy Folders:
####################################

# VSDK - v1 engine - Copy as a recursive folder:
# Starting from v4.5 VSDK was deprecated, so we check if it exists in the vcomponents folder.
if [ ! -d "$app_vcomp"/VSDK.framework ] ; then
  if [ -d /usr/local/lib/vcomponents/VSDK.framework ] ; then
	cp -R /usr/local/lib/vcomponents/VSDK.framework "$app_vcomp"
  fi
fi

# vresources
cp -R /usr/local/lib/vcomponents/vresources "$app_vcomp"


#####################################
# CHANGE executable_path for engine dylibs:
#####################################

	# change id of VSHARED
	install_name_tool -id "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvshared_fat_release.dylib

	# change id of VKERNEL
	install_name_tool -id "@executable_path/../vcomponents/libvkernel_fat_release.dylib" "$app_vcomp"/libvkernel_fat_release.dylib

	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib"   "@executable_path/../vcomponents/libvshared_fat_release.dylib" 	"$app_vcomp"/libvkernel_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/VSDK.framework/Versions/A/VSDK" "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" 	"$app_vcomp"/libvkernel_fat_release.dylib

	# change id of VCLIENT
	install_name_tool -id "@executable_path/../vcomponents/libvclient_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib

	# change id of VREPORT
	install_name_tool -id "@executable_path/../vcomponents/libvreport_fat_release.dylib" "$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib" 	"$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libwx_mac.dylib" "@executable_path/../vcomponents/libwx_mac.dylib" 							"$app_vcomp"/libvreport_fat_release.dylib

	# change id of WX (optionally)
	if [ -e "$app_vcomp"/libwx_mac.dylib ] ; then
		install_name_tool -id "@executable_path/../vcomponents/libwx_mac.dylib" "$app_vcomp"/libwx_mac.dylib
	fi

	# change id of VSDK.framework (optionally)
 	if [ -d "$app_vcomp"/VSDK.framework ] ; then
   		install_name_tool -id "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" "$app_vcomp"/VSDK.framework/Versions/A/VSDK
    fi
    
    
#####################################
# now we need update yet App/Xtras/V4MD.xtra  dll to also point new location of vcomponents folder.
#####################################
v4md_file="$app"/../Xtras/V4MD.xtra/Contents/MacOS/V4MD

	#
	# now we can change paths In the V4MD.Xtra
	#
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" 	"@executable_path/../vcomponents/libvshared_fat_release.dylib" 		"$v4md_file"
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib" 	"@executable_path/../vcomponents/libvkernel_fat_release.dylib" 		"$v4md_file"
	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib" 	"@executable_path/../vcomponents/libvclient_fat_release.dylib" 		"$v4md_file"
	install_name_tool -change "/usr/local/lib/vcomponents/libvreport_fat_release.dylib" 	"@executable_path/../vcomponents/libvreport_fat_release.dylib" 		"$v4md_file"

	install_name_tool -change "/usr/local/lib/vcomponents/VSDK.framework/Versions/A/VSDK" 	"@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" 	"$v4md_file"
